home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / GZIP107S.ZIP;1 / GZIP107.TAR / gzip-1.0.7 / gzexe.in < prev    next >
Encoding:
Text File  |  1993-03-18  |  3.3 KB  |  136 lines

  1. :
  2. #!/bin/sh
  3. # gzexe: compressor for Unix executables.
  4. # Use this only for binaries that you do not use frequently.
  5. #
  6. # The compressed version is a shell script which decompresses itself after
  7. # skipping $skip lines of shell commands.  We try invoking the compressed
  8. # executable with the original name (for programs looking at their name).
  9. # We also try to retain the original file permissions on the compressed file.
  10. # For safety reasons, gzexe will not create setuid or setgid shell scripts.
  11.  
  12. # Warning: the first line of this file must be either : or #!/bin/sh
  13. # The : is required for some old versions of csh.
  14.  
  15. x=`basename $0`
  16. if test $# = 0; then
  17.   echo compress executables. original file foo is renamed to foo~
  18.   echo usage: ${x} [-d] files...
  19.   echo   "   -d  decompress the executables"
  20.   exit 1
  21. fi
  22.  
  23. tmp=gz$$
  24. trap "rm -f $tmp; exit 1" 1 2 3 5 10 13 15
  25.  
  26. decomp=0
  27. res=0
  28. test "$x" = "ungzexe" && decomp=1
  29. if test "$1" = "-d"; then
  30.   decomp=1
  31.   shift
  32. fi
  33.  
  34. echo hi > zfoo1$$
  35. echo hi > zfoo2$$
  36. if test -z "`(${CPMOD-cpmod} zfoo1$$ zfoo2$$) 2>&1`"; then
  37.   cpmod=${CPMOD-cpmod}
  38. fi
  39. rm -f zfoo[12]$$
  40.  
  41. for i do
  42.   if test ! -f "$i" ; then
  43.     echo ${x}: $i not a file
  44.     res=1
  45.     continue
  46.   fi
  47.   if test $decomp -eq 0; then
  48.     if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then
  49.       echo "${x}: $i is already gzexe'd"
  50.       continue
  51.     fi
  52.   fi
  53.   if ls -l "$i" | grep '^...[sS]' > /dev/null; then
  54.     echo "${x}: $i has setuid permission, unchanged"
  55.     continue
  56.   fi
  57.   if ls -l "$i" | grep '^......[sS]' > /dev/null; then
  58.     echo "${x}: $i has setgid permission, unchanged"
  59.     continue
  60.   fi
  61.   if test "`basename $i`" = gzip; then
  62.     echo "${x}: cannot compress gzip itself"
  63.     continue
  64.   fi
  65.   if test -z "$cpmod"; then
  66.     cp -p "$i" $tmp 2>/dev/null || cp "$i" $tmp
  67.     if test -w $tmp 2>/dev/null; then
  68.       writable=1
  69.     else
  70.       writable=0
  71.       chmod u+w $tmp 2>/dev/null
  72.     fi
  73.   fi
  74.   if test $decomp -eq 0; then
  75.     sed 1q $0 > $tmp
  76.     cat >> $tmp <<'EOF'
  77. skip=18
  78. if tail +$skip $0 | gzip -cd > /tmp/gztmp$$; then
  79.   chmod 755 /tmp/gztmp$$
  80.   prog="`basename $0`"
  81.   if ln /tmp/gztmp$$ "/tmp/$prog" 2>/dev/null; then
  82.     trap '/bin/rm -f /tmp/gztmp$$ "/tmp/$prog"; exit $res' 0
  83.     (sleep 5; /bin/rm -f /tmp/gztmp$$ "/tmp/$prog") 2>/dev/null &
  84.     /tmp/"$prog" ${1+"$@"}; res=$?
  85.   else
  86.     trap '/bin/rm -f /tmp/gztmp$$; exit $res' 0
  87.     (sleep 5; /bin/rm -f /tmp/gztmp$$) 2>/dev/null &
  88.     /tmp/gztmp$$ ${1+"$@"}; res=$?
  89.   fi
  90. else
  91.   echo Cannot decompress $0; exit 1
  92. fi; exit $res
  93. EOF
  94.     gzip -cv9 "$i" >> $tmp || {
  95.       /bin/rm -f $tmp
  96.       echo ${x}: compression not possible for $i, file unchanged.
  97.       res=1
  98.       continue
  99.     }
  100.  
  101.   else
  102.     # decompression
  103.     skip=18
  104.     if sed -e 1d -e 2q "$i" | grep "^skip=[0-9]*$" >/dev/null; then
  105.       eval `sed -e 1d -e 2q "$i"`
  106.     fi
  107.     if tail +$skip "$i" | gzip -cd > $tmp; then
  108.       :
  109.     else
  110.       echo ${x}: $i probably not in gzexe format, file unchanged.
  111.       res=1
  112.       continue
  113.     fi
  114.   fi
  115.   rm -f "$i~"
  116.   mv "$i" "$i~" || {
  117.     echo ${x}: cannot backup $i as $i~
  118.     rm -f $tmp
  119.     res=1
  120.     continue
  121.   }
  122.   mv $tmp "$i" || cp -p $tmp "$i" 2>/dev/null || cp $tmp "$i" || {
  123.     echo ${x}: cannot create $i
  124.     rm -f $tmp
  125.     res=1
  126.     continue
  127.   }
  128.   rm -f $tmp
  129.   if test -n "$cpmod"; then
  130.     $cpmod "$i~" "$i" 2>/dev/null
  131.   elif test $writable -eq 0; then
  132.     chmod u-w $i 2>/dev/null
  133.   fi
  134. done
  135. exit $res
  136.